package com.lu.netty.io.core

import com.lu.netty.io.dispatcher.IStateListener
import java.io.Closeable

abstract class IoConnector : Closeable {

    private var ioState = IoState.INIT
    protected var retryTimes = 0
    private var onStateListener: IStateListener? = null
    protected fun setIoState(state: IoState) {
        if (ioState != state) {
            this.ioState = state
            onStateListener?.onStateChange(state)
        }
    }

    protected fun getIoState() = ioState
    fun canRetry(): Boolean {
        return retryTimes < NettyConfigure.maxRetryCount
    }

    fun checkCanConnect(): Boolean {
        return ioState != IoState.CONNECTING
                && ioState != IoState.CONNECTED
    }

    fun setOnStateChangeListener(listener: IStateListener) {
        this.onStateListener = listener
    }

    abstract fun start()
    abstract fun release()
}